import Vue from 'vue';
import VueRouter from 'vue-router';
import Layout from '@/views/Layout.vue';
import { getToken } from '@/utils/auth';
import routerView from '@/components/routerView.vue';
console.log('123');

Vue.use(VueRouter);
//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// buffer:是否缓存  hidden:是否显示  isRouter:是否是导航路由

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录-智慧财务管理系统' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/index.vue'),
        meta: { title: '首页', icon: 'el-icon-location', buffer: true, hidden: true, isRouter: false },
      },
    ]
  },
];
const asyncRoutes = JSON.parse(localStorage.getItem('router'));  //路由表
console.log(asyncRoutes);
// function buildChildRoutes(children) {
//   const childRoutes = [];
//   for (let i = 0; i < children.length; i++) {
//     const child = children[i];
//     const route = {
//       path: child.path,
//       name: child.name,
//       // component: child.children && child.children.length > 0 ? routerView : () => import(`@/views/${child.component}.vue`),
//       component: () => import(`@/views/${child.component}.vue`),
//       meta: { title: child.title, icon: child.icon, buffer: child.buffer, hidden: child.hidden, isRouter: child.isRouter },
//     };

//     if (child.children && child.children.length > 0) {
//       route.children = buildChildRoutes(child.children);
//     }
//     childRoutes.push(route);
//   }
//   return childRoutes;
// }
// if (asyncRoutes) {
//   for (let i = 0; i < asyncRoutes.length; i++) {
//     const ele = asyncRoutes[i];
//     const route = {
//       path: ele.path,
//       name: ele.name,
//       component: () => import(`@/views/${ele.component}.vue`),
//       // component: ele.children && ele.children.length > 0 ? routerView : () => import(`@/views/${ele.component}.vue`),
//       meta: { title: ele.title, icon: ele.icon, buffer: ele.buffer, hidden: ele.hidden, isRouter: ele.isRouter },
//     };

//     if (ele.children && ele.children.length > 0) {
//       route.children = buildChildRoutes(ele.children);
//     }
//     routes[1].children.push(route);

//   }
// }
// 生成单个路由对象的函数
function createRouteConfig(item) {
  const route = {
    path: item.path,
    name: item.name,
    component: item.children && item.children.length > 0 ? routerView : () => import(`@/views/${item.component}.vue`),
    meta: { title: item.title, icon: item.icon, buffer: item.buffer, hidden: item.hidden, isRouter: item.isRouter }
  };

  if (item.children && item.children.length > 0) {
    route.children = buildChildRoutes(item.children);
  }
  return route;
}

// 构建子路由的函数
function buildChildRoutes(children) {
  return children.map(child => createRouteConfig(child));
}

// 检查路由是否已存在
function routeExists(routeName) {
  return routes.some(route => route.name === routeName || (route.children && route.children.some(child => child.name === routeName)));
}
// 动态添加路由的函数
// export function addDynamicRoutes(asyncRoutes) {
//   if (asyncRoutes) 
//     asyncRoutes.forEach(ele => {
//       if (!routeExists(ele.name)) {
//         const route = createRouteConfig(ele);
//         routes[1].children.push(route);
//         router.addRoute(route); // 使用 addRoute 方法添加路由
//       }
//     });
//   }


  if (asyncRoutes) {
    asyncRoutes.forEach(ele => {
      if (!routeExists(ele.name)) {
        const route = createRouteConfig(ele);
        routes[1].children.push(route);
      }
    });
  }

console.log(routes, 'routes');
const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
});


router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (!getToken()) {
      next('/login');
    }
  }
  next();
  // 动态设置页面标题
  document.title = to.meta.title;
});
export default router;
